$(function(){
    $('.checkAll').click(function(){
        if($(this).is(':checked')){
            $('.content_table .checkinput').attr('checked','checked');
            $(this).attr('title','取消全选')
        }else{
            $('.content_table .checkinput').removeAttr('checked');
            $(this).attr('title','全选')
        }
    })
    //1 绑定添加按钮
    $('.add_btn').click(function(){
        layer.open({
        	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
            type:2,
            title:'添加用户页面',
            area:['600px','400px'],
            content:'initEditSysPerson.do?opera=add',
            maxmin: true,
            shadeClose: true
        });
    });

    //4 绑定查询按钮
    $('.sel_btn').click(function(){
        $("#treeId").val("");
        paging(1);
    });

    //5 绑定重置按钮
    $('.reset_btn').click(function(){
        $("#personName").val('');
        $("#personName").focus();
    });
    //6.删除选定的，多项删除
    $('.del_select_btn').click(function(){
        del_select_func(this);
    });
    //ajax初始化数据
    paging(1);
    //初始化部门树
    flashTreeNode();
});
edit_func=function(obj){
    var personId=$(obj).parent().parent().attr('personid');
    layer.open({
    	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'修改用户页面',
        area:['600px','400px'],
        content:'initEditSysPerson.do?opera=edit&personId='+personId,
        maxmin: true,
        shadeClose: true
    });
}
delete_func=function(obj){
    var person_Id=$(obj).parent().parent().attr('personid');
    if(confirm('确定删除该用户?')){
        $.ajax({
            url:"deletePerson.do",
            type:"post",
            dataType: 'json',
            data:{
                "personId":person_Id
            },
            beforeSend:function(){
                $(obj).attr("disabled","disabled");
            },
            error:function(data){
                layer.msg('出错了！！',{time:2000});
            },
            success:function(result){
                if(result.data){
                    layer.msg('删除成功', {time:1300});
                    $('.checkAll').removeAttr('checked');
                    paging(1);
                }else{
                    layer.msg('删除失败！', {time:1000});
                    $(obj).removeAttr("disabled");
                }
            }
        });
    }
}

function del_select_func(obj){
    var checkedDel = "";
    var arr=document.getElementsByName("checkradio");

    for(i=0;i<arr.length;i++){
        if(arr[i].checked){
            var a = arr[i].value;
            checkedDel+=a+",";
        }
    }
    if(checkedDel==""){
        layer.msg('先勾选选择框，再点击删除按钮！', {time:1300});
    }else{
        if(confirm('确定删除所有选中的用户?')){
            $.ajax({
                url:"deleteSelectSysPerson.do",
                type:"post",
                dataType: 'json',
                data:{
                    "checkedDel":checkedDel
                },
                beforeSend:function(){

                },
                error:function(data){
                    layer.msg('出错了！！',{time:2000});
                },
                success:function(result){
                    if(result.data){
                        layer.msg('删除成功', {time:1300});
                        $('.checkAll').removeAttr('checked');
                        paging(1);
                    }else{
                        layer.msg('删除失败！', {time:1000});
                        $(obj).removeAttr("disabled");
                    }
                }
            });
        }
    }
}


function paging(pageNo) {
    var pageSize = $("#pageSize  option:selected").text();
    var personName = $("#personName").val();
    var treeId = $("#treeId").val();
    $.ajax({
        url: "pagingPersonList.do",
        type: "post",
        dataType: 'json',
        data: {
            "iPageSize": pageSize,
            "iPageNo": pageNo,
            "personName": personName,
            "depId":treeId
        },
        error: function (data) {
            layer.msg('出错了！！',{time:2000});
        },
        success: function (jsons) {
            if (jsons != null) {
                $("#pageTable").html("");
                var pageList = jsons.pageData.pageList;
                for (var i = 0; i < pageList.length; i++) {
                    var partsForm =
                        '<tr personid="'+pageList[i].personId +'">' +
                        '<td class="checkNot"><input type="checkbox" value="' + pageList[i].personId + '" class="checkinput" name="checkradio" /></td>' +
                            // '<td title=' + (pageList[i].personId == null ? '' : pageList[i].personId) + '>' + (pageList[i].personId == null ? '' : pageList[i].personId) + '</td>' +
                        '<td title=' + (pageList[i].personName == null ? '' : pageList[i].personName) + '>' + (pageList[i].personName == null ? '' : pageList[i].personName) + '</td>' +
                        '<td title=' + (pageList[i].personCode == null ? '' : pageList[i].personCode) + '>' + (pageList[i].personCode == null ? '' : pageList[i].personCode) + '</td>' +
                        '<td title=' + (pageList[i].idendtityNo == null ? '' : pageList[i].idendtityNo) + '>' + (pageList[i].idendtityNo == null ? '' : pageList[i].idendtityNo) + '</td>' +
                        '<td title=' + (pageList[i].officePhone == null ? '' : pageList[i].officePhone) + '>' + (pageList[i].officePhone == null ? '' : pageList[i].officePhone) + '</td>' +
                        '<td title=' + (pageList[i].mobilePhone == null ? '' : pageList[i].mobilePhone) + '>' + (pageList[i].mobilePhone == null ? '' : pageList[i].mobilePhone) + '</td>' +
                        '<td title=' + (pageList[i].email == null ? '' : pageList[i].email) + '>' + (pageList[i].email == null ? '' : pageList[i].email) + '</td>' +
                        '<td title=' + (pageList[i].personState == "9ea" ? "启用" : "关闭") + '>' + (pageList[i].personState == "9ea" ? "启用" : "关闭") + '</td>' +
                        '<td title=' + (pageList[i].cmpName == null ? '该公司已经删除' : pageList[i].cmpName) + '>' + (pageList[i].cmpName == null ? '该公司已经删除' : pageList[i].cmpName) + '</td>' +
                        '<td title=' + (pageList[i].depName == null ? '' : pageList[i].depName) + '>' + (pageList[i].depName == null ? '' : pageList[i].depName) + '</td>' +
                        '<td title=' + (pageList[i].roleName == null ? '该角色已经删除' : pageList[i].roleName) + '>' + (pageList[i].roleName == null ? '该角色已经删除' : pageList[i].roleName) + '</td>' +
                        '<td title=' + (pageList[i].remark == null ? '' : pageList[i].remark) + '>' + (pageList[i].remark == null ? '' : pageList[i].remark) + '</td>' +
                        '<td><i class="Edit content_current edit_btn" title="编辑"></i><i class="Del content_current delete_btn" title="删除"></i></td>' +
                        '</tr>';
                    $(partsForm).appendTo("#pageTable");
                }
                //3 绑定编辑按钮
                $('.edit_btn').click(function(){
                    edit_func(this);
                });
                $('.delete_btn').click(function(){
                    delete_func(this);
                });

                //处理footer 分页
                var totalCount = jsons.pageData.totalCount;
                var pageSize = jsons.pageData.pageSize;
                var totalPage = jsons.pageData.totalPage;
                var pageNo = jsons.pageData.pageNo;
                dealFooterPage(totalCount, pageSize, totalPage, pageNo);

            }
        }
    });
}


//左侧部门树
function flashTreeNode(){

    var setting = {
        data : {
            key : {
                url : "u"
            },
            simpleData : {
                enable : true
            }
        },
        callback : {
            onClick : onClick
        }
    };

    $.ajax({
        type: 'POST',
        dataType : "json",
        data:{
        },
        url: "initDepPersonTree.do",//请求的action路径
        error: function () {//请求失败处理函数
            layer.msg('请求失败！',{time:2000});
        },
        success:function(result){ //请求成功后处理函数。
            var treeNodes = eval("("+result.lstTree+")");
            //var treeNodes = result.lstTree;   //把后台封装好的简单Json格式赋给treeNodes
            $.fn.zTree.init($("#treeWay"), setting, treeNodes);
        }
    });

    function onClick(event, treeId, treeNode, clickFlag){
        $("#personName").val('');
        $("#treeId").val("");
        $("#treeId").val(treeNode.id);
        //document.getElementById("privilegeName").innerHTML=treeNode.name;
        //$('.checkAll').removeAttr('checked');
        paging(1);
    }
}